var util = require('../../utils/util.js');
var utilMd5 = require('../../utils/md5.js');
var app = getApp();
var page = 1;
var loadingMore = false;
var hasMore = true;

Page({

   data: {
    postuid: "",
     uid:0,
     token: '',
     picker_arr:[],//picker中range属性值
     picker_index:0,//picker中value属性值
     id_arr:[],//存储id数组
     result:"",//form提交最终结果
     tc:1,
     items: [
      { name: '男', value: '男' },
      { name: '女', value: '女', checked: 'true' },
      ]
   },
   /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var tc = app.get_tc();
    var titlename = tc == 1 ? '錄入信息 ' : '录入信息';
    that.setData({
      tc: tc
    })
    wx.setNavigationBarTitle({
      title: titlename,
    })
    hasMore = true;
     page = 1;
     this.setData({
      postuid:options.orgid
     });
     //获取颜色配置
     app.getColorSetData(function (color) {
       that.setData({
         colorSet: color
       })
       that.resetNavColor();
     });
     this.getLoginUserInfo('');
    //  this.getauthtype();
    this.getgonghuoshang(1);
    this.getauthtype();
  },

  getLoginUserInfo: function(inviteuid){
    var that = this;
    //调用应用实例的方法获取全局数据
    app.LoginUserInfo(function (res) {
      if (res && res != -1) {
        //更新数据
        that.setData({
          uid: res.uid,
          token: res.token
        })
      } else {
        app.reAuthorize(reGetUserInfo);
      }
    }, inviteuid);
  },


  //下拉选项改变值
    bindPickerChange: function(e) {//选项改变触发事件

    this.setData({
      picker_index: e.detail.value
    })
  },


  ////表单提交触发事件
  formSubmit:function(e){
    app.saveFormId(e.detail.formId);
    var that = this;
    var res = e.detail.value;
    var username = res.username;
    var mobile = res.mobile;
    var age = res.age;
    var gender = res.gender;
    var selector = this.data.id_arr[res.selector];
    console.log("selector",selector);
    if(username == ""){
      var contentname = this.data.tc == 1 ? '姓名不能為空' : '姓名不能为空';
       wx.showModal({
            title: '提示',
            content: contentname,
            showCancel: false,
          })
      return; 
    }else if(mobile == ""){
      var contentmobile = this.data.tc == 1 ? '手機號不能為空' : '手机号不能为空';
       wx.showModal({
            title: '提示',
            content: contentmobile,
            showCancel: false,
          })
      return;
    }else if(selector == "0" || selector == ""){
      var contentselector = this.data.tc == 1 ? '請選擇認證類型' : '请选择认证类型';
       wx.showModal({
        title: '提示',
        content: contentselector,
        showCancel: false,
      })
      return;
    }
    //提交用户认证请求
    var privatekey = utilMd5.hexMD5(that.data.uid + 'userauth' + that.data.token);
     util.api({
      method: "post",
      data: {
        ac: "userauth",
        uid: that.data.uid,
        age: age,
        sex: gender,
        realname: username,
        phone: mobile,
        catatype: "10",
        orgid: that.data.postuid,
        privatekey: privatekey,
        selector:selector
        
      },
      
      success: function (responseData) {
        console.log("调试--提交用户认证请求接口:", responseData.data);
       if(responseData.data.res == false){
        var contentfalse = this.data.tc == 1 ? '認證失敗,請聯系管理員' : '认证失败,请联系管理员';
         wx.showModal({
            title: '提示',
            content: contentfalse,
            showCancel: false,
          })
       }else{
        // var contentsucess = this.data.tc == 1 ? '認證成功' : '认证成功';
        //  wx.showModal({
        //   title: '提示',
        //   content: contentsucess,
        //   showCancel: false,
        // })
         var contentsucess = this.data.tc == 1 ? '認證成功' : '认证成功';
         app.dialogToast(contentsucess, "success", function () {
           wx.reLaunch({ url: '/pages/mine/mine' });
         })
       }
        
      },
      fail: function (e) {
        var contentfail = this.data.tc == 1 ? '網絡異常,請稍候再試' : '网络异常,请稍候再试';
         wx.showModal({
          title: '提示',
          content: contentfail,
          showCancel: false,
        })
       
      }
    });
  },


   //取用户认证分类
   getauthtype:function(){
  
    var that = this;
    
    util.api({
      method: "get",
      data: {
        ac: "getauthtype",
        binduid: that.data.uid,
        
      },
      
      success: function (responseData) {
        console.log("调试--取用户认证分类接口:", responseData.data);
        var result = responseData.data;
        var picker_arr = [that.data.tc == 1 ? '請選擇' : '请选择'],
            id_arr     = ["0"];
        for(var i = 0; i < result.length; i++){
          var id = result[i].id;
          var title = result[i].title;
  
     
          picker_arr.push(title);
          id_arr.push(id);
           
      
          
        }
        that.setData({
          picker_arr:picker_arr,
          id_arr:id_arr
        });
        

      }
    });

    

    
    

    
   


  },

  getgonghuoshang:function(cpage){
    var that = this;
    var params = {
      publickey: util.publiccode,
      ac: "getghs",
      orgid: util.orgid,
      page: cpage,
      num: 10
    }
    util.api({
      data: params,
      success: function (responseData) {
        var result = responseData.data;
        console.log("供货商数据：", result);
        var picker_arr = [that.data.tc == 1 ? '請選擇' : '请选择'],
            id_arr     = ["0"];
        for(var i = 0; i < result.length; i++){
          if (result[i].gcataid == '189' || result[i].gcataid == '237' || result[i].gcataid == '412' ){
            var uid = result[i].uid;
            var title = result[i].username;
    
      
            picker_arr.push(title);
            id_arr.push(uid);
          }
        }
        that.setData({
          picker_arr:picker_arr,
          id_arr:id_arr
        });
  
      }
    });
  },
  resetNavColor: function () {
    var bg_color = '#d8202d';
    var fg_color = '#ffffff';
    if (this.data.colorSet) {
      var colorJO = this.data.colorSet;
      bg_color = colorJO.titlebg;
      fg_color = colorJO.titlefont;
    }
    wx.setNavigationBarColor({
      frontColor: fg_color,
      backgroundColor: bg_color
    })
    this.setData({
      frontColor: fg_color,
      backgroundColor: bg_color
    })
  },
})